import Vue from "vue";
import VueRouter from "vue-router";
// 导入 路由实例
import store from "@/store";
Vue.use(VueRouter);

import home from '../views/home/home.vue'
import category from '../views/category/category.vue'
import shopcart from '../views/shopcart/shopcart.vue'
import user from '../views/user/user.vue'
import productlist from '../views/productlist/productlist.vue'
import login from '../views/login/login.vue'
import register from '../views/register/register.vue'

const routes = [{
        path: '/',
        name: 'Home',
        component: home,
        meta: {
            index: 1
        }
    },
    {
        path: '/category',
        name: 'Category',
        component: category,
        meta: {
            index: 1
        }
    }, {
        path: '/shopcart',
        name: 'Shopcart',
        component: shopcart,
        meta: {
            auth: true,
            index: 1
        }
    }, {
        path: '/register',
        name: 'Register',
        component: register,
        meta: {
            auth: true,
            index: 1
        }
    }, {
        path: '/user',
        name: 'User',
        component: user,
        meta: {
            auth: true,
            index: 1
        }
    },
    {
        path: '/productlist',
        name: 'Productlist',
        component: productlist,
        meta: {
            index: 2
        }
    }, {
        path: '/login',
        name: 'Login',
        component: login,
        meta: {
            index: 2
        }
    },
    {
        // /:id 提供id 参数传递
        path: '/detail/:id',
        name: 'Detail',
        component: () =>
            import ('@/views/productDetail/productDetail.vue'),
        meta: {
            index: 3
        }
    },
    {
        path: '/proxy',
        name: 'Proxy',
        component: () =>
            import ('../views/login/proxy.vue')
    }

];

const router = new VueRouter({
    mode: 'history',
    routes,
});
router.beforeEach((to, from, next) => {
    // 判断当前用户 已登录 再次访问 登录界面 直接跳转到 个人中心页面
    if (to.name === 'Login' && store.getters.getIsLogin) {
        next('/user')
    }
    // console.log('from:', from);
    if (to.meta.auth) { //是否校验用户是否登录的页面
        //判断 用户是否登录
        // console.log(store.getters);
        if (store.getters.getIsLogin) {
            next()
        } else {
            //未登录跳转到登陆页面
            next({ path: '/login' })
        }
    } else {
        next()
    }
})
export default router;